import msg from "./msg";
import { login, updateUserInfo } from "./request/api";
interface UserInfo {
	name: string,
	code: string,
	class_id: number,
	className: string,
}
interface userAtt {
	userId: number,
	name: string,
	code: string,
	className: string,//班级名称
	class_id: number,
	openid: string,
	grade: string,
	roleCode:string;
	roleName:string;
}
export default class User {
	userId = 0 as number;
	name = '' as string;
	code = '' as string;
	grade = '' as string;
	className = '' as string;//班级名称
	class_id = 0 as number;
	openid = '' as string;
	roleCode='' as string;
	roleName='' as string;
	constructor() {
		//console.log('user=',wx.getStorageSync('user'));
		if (!this.cheakLogin()) return;
		this.dataInit();
	};
	public async Login(code: string) {
		return new Promise(async (resolve, reject) => {
			await login({
				code: code
			}).then((res: any) => {
				if (!res.data.code) return;
				this.setAtt(res.data.data);
			});
			resolve('');
		});
	};
	public setAtt(data: userAtt) {
		this.userId = data.userId;
		this.openid = data.openid;
		this.name = data.name;
		this.code = data.code;
		this.className = data.grade + data.className;
		this.grade = data.grade;
		this.class_id=data.class_id;
		this.roleCode=data.roleCode;
		this.roleName=data.roleName;
		this.setStorage();
	}
	//检查是否可以签到，报名，即基本信息是否完善
	private cheakValid(): boolean {
		if (!this.cheakLogin()) return false;
		if (this.name && this.code && this.class_id) return true;
		return false;
	};
	//设置全局变量
	public setStorage() {
		wx.setStorageSync('user', this);
	};
	//拿取用户可以填写的数据
	public getInfo() {
		return {
			name: this.name,
			code: this.code,
			class_id: this.class_id,
			className:  this.className,
		};
	};
	public async updateInfo(param: UserInfo) {
		this.name = param.name;
		this.code = param.code;
		this.class_id = param.class_id;
		return new Promise(async (resolve, reject) => {
			await updateUserInfo({ name: this.name, code: this.code, class_id: this.class_id, userId: this.userId })
				.then((res: any) => {
					if (res.data.code == 200){
						this.setAtt(res.data.data);
						msg.show('绑定学生信息成功', '');
					}
					else msg.show('绑定学生信息失败', '请检查姓名,学号,班级是否有误');
					resolve(res.data.code);
				});
		}).catch((e:any) => {});
	};
	//检查是否已登录
	public cheakLogin(param: number = 0): boolean {
		let user_temp: userAtt = wx.getStorageSync('user');
		//console.log('user=',user_temp);
		//未登录
		if (user_temp.userId == undefined || user_temp.userId == 0) {
			if (param != 0) {
				msg.confirm('您尚未登陆', '是否进行登陆/注册')
					.then(() => {
						wx.navigateTo({
							url: "/pages/user/login/login",
						})
					})
			}
			return false;
		}
		//已经登陆
		else {
			return true;
		}
	};
	//登出
	public logOut(): void {
		wx.removeStorageSync('user');
		this.dataInit();
	};
	private dataInit(): void {
		let user_temp: User = wx.getStorageSync('user');
		this.userId = user_temp.userId || 0;
		this.openid = user_temp.openid || '';
		this.name = user_temp.name || '';
		this.code = user_temp.code || '';
		this.className = user_temp.className || '';
		this.grade = user_temp.grade || '';
		this.class_id = user_temp.class_id || 0;
		this.roleCode=user_temp.roleCode || '0';
		this.roleName=user_temp.roleName || '普通学生';
	};
}
export {
	UserInfo,
}